package game.module.sss.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.hall.logic.QipaiDataManager;
import game.module.sss.bean.SssRoom;
import game.module.sss.logic.SssDataManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageSss.PushSssForceDismissRoom;
import lua.LuaMessageSss.S2CSssForceDismisroom;
import lua.LuaMessageSss.C2SSssForceDismisroom;

@MsgCodeAnn(msgcode = C2SSssForceDismisroom.id, accessLimit = 500)
public class SssDismissRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(SssDismissRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		int playerId = hero.getId();
		logger.info("sss dismiss room,playeyId={}", playerId);
		// 是否在房间中
		boolean isInRoom = SssDataManager.getInstance().isPlayerInRoom(playerId);
		if (!isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is room exist
		SssRoom ddzRoom = SssDataManager.getInstance().getSssRoomByPlayerId(playerId);
		if (ddzRoom == null) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is room master
		List<Integer> playerIdList = ddzRoom.getPlayerList();
		PushSssForceDismissRoom pushMsg = new PushSssForceDismissRoom();
		if (ddzRoom.getPlayerList().get(0) != playerId) {
			pushMsg.player_index = (byte) playerIdList.indexOf(playerId);
			Integer play = new Integer(playerId);
			for (Integer aPlayerId : playerIdList) {
				PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
				if (pr != null && pr.isChannelActive()) {
					pr.writeAndFlush(pushMsg.build(pr.alloc()));
				}
			}
			playerIdList.remove(play); // 移除玩家
			SssDataManager.getInstance().removePlayer(play);//移除玩家
			
			return;
		}
		// push
		pushMsg.player_index = (byte) playerIdList.indexOf(playerId);
		for (Integer aPlayerId : playerIdList) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
		// do
		SssDataManager.getInstance().removeRoom(ddzRoom.getUuid());
		QipaiDataManager.getInstance().removeRoomUuid(ddzRoom.getUuid());
		// send
		S2CSssForceDismisroom respMsg = new S2CSssForceDismisroom();
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
